<?php
namespace Common\Model;
use Think\Model;
class CustModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_cust'; 
	private $DistributeLevel = 1;
	public $Referees = array();
	public $Distributor = array();
	
	public function getCustList($data){
		$map = array();
		if(!empty($data['searchKey'])){
			$where['nickname']  = array('like','%'.$data['searchKey'].'%');
			$where['realname']  = $data['searchKey'];
			$where['pinyinname']  = $data['searchKey'];
			$where['email']  = $data['searchKey'];
			if(\validate::isMobilePhoneNumber($data['searchKey'])) $where['mobile']  = $data['searchKey'];
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		}
		
		if($data['statusFilter']==="1" || $data['statusFilter']==="0" || $data['statusFilter']==="2" || $data['statusFilter']==="3")
			$map['status'] = $data['statusFilter'];
		
		if(!empty($data['area'])){
			$map['area']  = array('like' , $data['area'] . "%");
		}
		
		if($data['sex'] === "0" || $data['sex'] === "1"){
			$map['sex']  = $data['sex'];
		}
		
		if(!empty($data['custtype']) && is_array($data['custtype'])){
			$map['custtype']  = array('in' , $data['custtype']);
		}
		
		$total = $this->where($map)->count();
		$fields = array('custid','nickname','realname','firstname','secondname','pinyinname','custtype','idenum','mobile',
				'status','sex','age','country','zone','area','address','hometown','email','signature','description','lastloginip','logintimes','isonline','issuper','createtime','updatetime');
		
		if(empty($data['sortField']))$data['sortField'] = "custid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else 
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		if(empty($result)) $result = array();
		
		$pages = ($total==0) ? 0 : (!empty($data['pageSize']) ? ceil($total/$data['pageSize']) : 1);
		
		return array('total'=>$total , 'data'=>$result , 'pages'=>$pages);
	}
	
	
	public function getCustListByFiled($data){
		$map = array();
		if(!empty($data['searchKey']) && !empty($data['field'])){
			$field = $data['field'];
			$map['qyx_cust.'.$field]  = array('like','%'.$data['searchKey'].'%');
		}
	
		if(!empty($data['custtype'])){
			$map['qyx_cust.custtype']  = $data['custtype'];
		}
		
		if($data['iskitchener'] === 0 || $data['iskitchener'] === 1){
			$map['qyx_cust_ext.iskitchener']  = $data['iskitchener'];
		}
		
	
		$total = $this->join('left join qyx_cust_ext on qyx_cust.custid=qyx_cust_ext.custid')->where($map)->count();
		$total = (int)$total;
		$fields = array('qyx_cust.custid','qyx_cust.nickname','qyx_cust.realname','qyx_cust.pinyinname','qyx_cust.custtype','qyx_cust.idenum','qyx_cust.mobile','qyx_cust.username',
				'qyx_cust.status','qyx_cust.sex','qyx_cust.age','qyx_cust.country','qyx_cust.zone','qyx_cust.area','qyx_cust.address','qyx_cust.email','qyx_cust.signature',
				'qyx_cust.description','qyx_cust.lastloginip','qyx_cust.logintimes','qyx_cust.isonline','qyx_cust.createtime','qyx_cust.updatetime',
				'qyx_cust_ext.*');
	
		if(empty($data['sortField']))$data['sortField'] = "qyx_cust.custid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
	
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->join('left join qyx_cust_ext on qyx_cust.custid=qyx_cust_ext.custid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else
			$result = $this->join('left join qyx_cust_ext on qyx_cust.custid=qyx_cust_ext.custid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
	
		if(empty($result)) $result = array();
		
		$pages = ($total==0) ? 0 : (!empty($data['pageSize']) ? ceil($total/$data['pageSize']) : 1);
	
		return array('total'=>$total , 'data'=>$result , 'pages'=>$pages);
	}
	
	
	public function addCust($data){
		$result = $this->add($data);
		return $result;
	}
	
	public function emptyCustMobile($mobile){
		$map['mobile'] = $mobile;
		$data['mobile'] = 0;
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	public function emptyCustEmail($email){
		$map['email'] = $email;
		$data['email'] = "";
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	public function updateCust($data){
		if(isset($data['email'])){
			$m['custid'] = array('neq' , $data['custid']);
			$m['email'] = $data['email'];
			$res = $this->where($m)->find();
			if(!empty($res)){
				return -1;
			}
		}
		
		$map['custid'] = $data['custid'];
		//if(!empty($data['birthday']))$data['age'] = \tools::getAge($data['birthday']);
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	public function getMaxIdeNum(){
		$result = $this->field('max(idenum) as idenum')->find();
		return $result;
	}
	
	public function getCustInfoById($custid , $map = array()){
		if(empty($custid)) return array();
		$map['custid'] = $custid;
		$fields = array('custid','nickname','realname','firstname','secondname','pinyinname','custtype','password','idenum',
				'status','sex','birthday','age','country','zone','area','address','hometown','mobile','email','username','signature','description',
				'auth','webauth','lastloginip','logintimes','isonline','issuper','pid','createtime','updatetime');
		$result = $this->field($fields)->where($map)->find();
		return $result;
	}
	
	public function getCustInfoByMobile($mobile){
		if(empty($mobile)) return array();
		$map['mobile'] = $mobile;
		$fields = array('custid','nickname','realname','firstname','secondname','pinyinname','custtype','password','idenum',
				'status','sex','birthday','age','country','zone','area','address','hometown','mobile','username','email','signature','description',
				'auth','webauth','lastloginip','logintimes','isonline','issuper','pid','createtime','updatetime');
		$result = $this->field($fields)->where($map)->find();
		return $result;
	}
    public function getCustInfoByOnlyUsername($username){
        if(empty($mobile)) return array();
        $map['username'] = $username;
        $fields = array('custid','nickname','realname','firstname','secondname','pinyinname','custtype','password','idenum',
            'status','sex','birthday','age','country','zone','area','address','hometown','mobile','username','email','signature','description',
            'auth','webauth','lastloginip','logintimes','isonline','issuper','pid','createtime','updatetime');
        $result = $this->field($fields)->where($map)->find();
        return $result;
    }
	
	public function getCustInfoByEmail($email){
		if(empty($email)) return array();
		$map['email'] = $email;
		$fields = array('custid','nickname','realname','firstname','secondname','pinyinname','custtype','password','idenum',
				'status','sex','birthday','age','country','zone','area','address','hometown','mobile','username','email','signature','description',
				'auth','webauth','lastloginip','logintimes','isonline','issuper','pid','createtime','updatetime');
		$result = $this->field($fields)->where($map)->find();
		return $result;
	}
	
	public function searchCustInfo($username){
		if(empty($username)) return array();
		$map = array();
		if(\validate::isMobilePhoneNumber($username)) $where['mobile']  = $username;
		if(\validate::isUnsignedInt($username)) $where['idenum']  = $username;
		$where['email']  = $username;
		$where['nickname']  = $username;
        $where['username']  = $username;
		$where['_logic'] = 'or';
		$map['_complex'] = $where;
		$fields = array('custid','nickname','realname','firstname','secondname','pinyinname','custtype','password','idenum',
				'status','sex','birthday','age','country','zone','area','address','hometown','mobile','username','email','signature','description',
				'auth','webauth','lastloginip','logintimes','isonline','issuper','pid','createtime','updatetime');
		$result = $this->field($fields)->where($map)->select();//->limit(0 , 20)
		return $result;
	}
	
	public function getCustInfoByUserName($username){
		if(empty($username)) return array();
		$map = array();
		if(\validate::isMobilePhoneNumber($username)) $where['mobile']  = $username;
		if(\validate::isUnsignedInt($username)) $where['idenum']  = (int)$username;
		$where['email']  = $username;
        $where['username']  = $username;
		$where['_logic'] = 'or';
		$map['_complex'] = $where;
		$fields = array('custid','nickname','realname','firstname','secondname','pinyinname','custtype','password','idenum',
				'status','sex','birthday','age','country','zone','area','address','hometown','mobile','username','email','signature','description',
				'auth','webauth','lastloginip','logintimes','isonline','issuper','pid','createtime','updatetime');
		$result = $this->field($fields)->where($map)->find();
		return $result;
	}
	
	public function deleteCustById($custid){
		$result = $this->where(array('custid'=>$custid))->delete();
		return $result;
	}
	
	public function getRefereeById($custid , $level=3){
		$custInfo = $this->getCustInfoById($custid);
		if($custInfo['pid']){
			$pcustInfo = $this->getCustInfoById($custInfo['pid']);
			if(!empty($pcustInfo)){
				$pcustInfo['level'] = $this->DistributeLevel;
				$this->Referees[] = $pcustInfo;
				$this->DistributeLevel++;
				if($this->DistributeLevel <= $level){
					$this->getRefereeById($pcustInfo['custid'] , $level);
				}
			}
		}
	}
	
	private function getCustInfoByPIds($pids){
		$map['pid'] = array('in',$pids);
		$fields = array('custid','nickname','realname','firstname','secondname','pinyinname','custtype','password','idenum',
				'status','sex','birthday','age','country','zone','area','address','hometown','mobile','username','email','signature','description',
				'auth','webauth','lastloginip','logintimes','isonline','issuper','pid','createtime','updatetime');
		$custInfo = $this->field($fields)->where($map)->select();
		return $custInfo;
	}
	
	public function getDistributorById($custid , $level=1){
		$pids = array($custid);
		$custInfos1 = $this->getCustInfoByPIds($pids);
		if(!empty($custInfos1)){
			if($level == 1){
				$this->Distributor[$level] = $custInfos1;
			}
			
			$pids = array();
			foreach($custInfos1 as $custInfo){
				$pids[] = $custInfo['custid'];
			}
			
			$custInfos2 = $this->getCustInfoByPIds($pids);
			if(!empty($custInfos2)){
				if($level == 2){
					$this->Distributor[$level] = $custInfos2;
					return;
				}
				
				$pids = array();
				foreach($custInfos2 as $custInfo){
					$pids[] = $custInfo['custid'];
				}
				
				$custInfos3 = $this->getCustInfoByPIds($pids);
				if(!empty($custInfos3)){
					if($level == 3){
						$this->Distributor[$level] = $custInfos3;
						return;
					}
				}
			}
		}
	}
}


